//
//  QWPopMaskView.m
//  Pods
//
//  Created by kevin on 16/1/5.
//
//

#import "QWPopMaskView.h"

@interface QWPopMaskView () {
    //是否已移除
    BOOL isDismissed;
}

@property (nonatomic, copy)void(^dismission)(void);
@property (nonatomic, strong, readwrite)UIView *contentView;

@end

@implementation QWPopMaskView

#pragma mark UIResponder Method

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];
    
    UITouch *touch = [touches anyObject];
    CGPoint touchPosition = [touch locationInView:self];
    
    if (_tapToDismiss && [self shouldTapToDismissWithTouchPosition:touchPosition]) {
        [self doDismiss];
    }
}

#pragma mark Base Methods
- (void)loadView {
    
}

- (void)maskWillAppear {
    // do noting in base
}

- (void)maskDoAppear {
    // do noting in base
}

- (void)maskDidAppear {
    // do noting in base
}

- (void)maskWillDisappear {
    // do noting in base
}

- (void)maskDoDisappear {
    // do noting in base
}

- (void)maskDidDisappear {
    // do noting in base
}

- (BOOL)shouldTapToDismissWithTouchPosition:(CGPoint)touchPosition {
    // default return YES
    return YES;
}

#pragma mark Property Methods

- (UIView *)contentView {
    
    if (!_contentView) {
        _contentView = [[UIView alloc]init];
        _contentView.frame = self.bounds;
        [self addSubview:_contentView];
    }
    
    return _contentView;
}

#pragma mark Inerface Methods
- (void)showInView:(UIView *)view offsetInsets:(UIEdgeInsets)offsetInsets maskColor:(UIColor *)maskColor completion:(void(^)(void))completion dismission:(void(^)(void))dismission {
    
    self.dismission = dismission;
    
    CGRect frame = view.bounds;
    frame.origin.x += offsetInsets.left;
    frame.origin.y += offsetInsets.top;
    frame.size.width -= offsetInsets.left+offsetInsets.right;
    frame.size.height -= offsetInsets.top+offsetInsets.bottom;
    
    self.backgroundColor = [UIColor clearColor];
    self.frame = frame;
    self.clipsToBounds = YES;
    [view addSubview:self];
    
    [self loadView];
    
    [self maskWillAppear];
    
    [UIView animateWithDuration:_animationDuration?_animationDuration:.3f
                          delay:0
                        options:UIViewAnimationOptionBeginFromCurrentState|UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         [self maskDoAppear];
                         self.backgroundColor = maskColor;
                     }
                     completion:^(BOOL finished) {
                         if (finished) {
                             [self maskDidAppear];
                             
                             if (completion) {
                                 completion();
                             }
                         }
                     }];
}

- (void)doDismiss {
    
    if (isDismissed) {
        return;
    }
    else {
        isDismissed = YES;
    }
    
    [self maskWillDisappear];
    
    [UIView animateWithDuration:_animationDuration?_animationDuration:.3f
                          delay:0
                        options:UIViewAnimationOptionBeginFromCurrentState|UIViewAnimationCurveEaseInOut
                     animations:^{
                         
                         [self maskDoDisappear];
                         self.backgroundColor = [UIColor clearColor];
                     }
                     completion:^(BOOL finished) {
                         
                         [self maskDidDisappear];
                         
                         if (_dismission) {
                             _dismission();
                         }
                         
                         [self removeFromSuperview];
                     }
     ];
}



@end
